package com.mdlib.helper.ui.widget.springview;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ljlib.help.R;

/**
 * 作者：HQY on 16/12/3 10:35
 * 邮箱：hqy_xz@126.com
 */

public class HelperHeads extends BaseHeader {
    private AnimationDrawable animationPull;
    private AnimationDrawable animationPullFan;
    private AnimationDrawable animationRefresh;

    private Context context;
    private ImageView header_img;
    private int[] pullAnimSrcs = new int[]{R.drawable.iv_helper_heads_one, R.drawable.iv_helper_heads_two};
    private int[] refreshAnimSrcs = new int[]{R.drawable.iv_helper_heads_one, R.drawable.iv_helper_heads_two};

    public HelperHeads(Context context) {
        this(context, null, null);
    }

    public HelperHeads(Context context, int[] pullAnimSrcs, int[] refreshAnimSrcs) {
        this.context = context;
        if (pullAnimSrcs != null) this.pullAnimSrcs = pullAnimSrcs;
        if (refreshAnimSrcs != null) this.refreshAnimSrcs = refreshAnimSrcs;
        animationPull = new AnimationDrawable();
        animationPullFan = new AnimationDrawable();
        animationRefresh = new AnimationDrawable();
        for (int i = 1; i < this.pullAnimSrcs.length; i++) {
            animationPull.addFrame(ContextCompat.getDrawable(context, this.pullAnimSrcs[i]), 100);
            animationRefresh.setOneShot(true);
        }
        for (int i = this.pullAnimSrcs.length - 1; i >= 0; i--) {
            animationPullFan.addFrame(ContextCompat.getDrawable(context, this.pullAnimSrcs[i]), 100);
            animationRefresh.setOneShot(true);
        }
        for (int src : this.refreshAnimSrcs) {
            animationRefresh.addFrame(ContextCompat.getDrawable(context, src), 150);
            animationRefresh.setOneShot(false);
        }
    }

    @Override
    public View getView(LayoutInflater inflater, ViewGroup viewGroup) {
        View view = inflater.inflate(R.layout.helper_heads, viewGroup, true);
        header_img = (ImageView) view.findViewById(R.id.iv_helper_refresh);
        if (pullAnimSrcs != null && pullAnimSrcs.length > 0)
            header_img.setImageResource(pullAnimSrcs[0]);
        return view;
    }

    @Override
    public void onPreDrag(View rootView) {
    }

    @Override
    public void onDropAnim(View rootView, int dy) {
        int maxw = DensityUtil.dip2px(context, 45);
        float w = maxw * Math.abs(dy) / rootView.getMeasuredHeight();
        if (w > maxw) return;
        ViewGroup.LayoutParams layoutParams = header_img.getLayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        header_img.setLayoutParams(layoutParams);
    }

    @Override
    public void onLimitDes(View rootView, boolean upORdown) {
        if (!upORdown) {
            header_img.setImageDrawable(animationPull);
            animationPull.start();
        } else {
            header_img.setImageDrawable(animationPullFan);
            animationPullFan.start();
        }
    }

    @Override
    public void onStartAnim() {
        header_img.setImageDrawable(animationRefresh);
        animationRefresh.start();
    }

    @Override
    public void onFinishAnim() {
        if (pullAnimSrcs != null && pullAnimSrcs.length > 0)
            header_img.setImageResource(pullAnimSrcs[0]);
    }
}
